package com.yilin.tms.order.controller;

import com.yilin.commons.util.ArrayUtil;
import com.yilin.tms.core.application.PageData;
import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.commons.annotation.Explain;
import com.yilin.tms.core.commons.annotation.NotNull;
import com.yilin.tms.core.commons.annotation.SafeVerify;
import com.yilin.tms.core.commons.utils.ParamUtil;
import com.yilin.tms.core.entity.capital.TradeRecord;
import com.yilin.tms.core.entity.order.payment.OrderPaymentCost;
import com.yilin.tms.core.entity.order.payment.OrderRefundCost;
import com.yilin.tms.core.entity.order.payment.OrderSettlementCost;
import com.yilin.tms.core.entity.order.payment.ServicePaymentCost;
import com.yilin.tms.core.remote.ICapitalRemote;
import com.yilin.tms.order.service.payment.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


/**
 * 货单控制器
 *
 * @author zhangjinyuan
 * @date 2022/10/29
 */
@RestController
@RequestMapping("/order/payment/")
public class PaymentController extends BaseController {

    @Resource
    private IOrderPaymentCostService orderPaymentCostService;
    @Resource
    private IOrderSettlementCostService orderSettlementCostService;
    @Resource
    private IOrderRefundCostService orderRefundCostService;
    @Resource
    private IServicePaymentCostService servicePaymentCostService;
    @Resource
    private ICalculationService calculationService;

    @Resource
    private ICapitalRemote capitalRemote;


    @PostMapping(value = "getOrderPaymentPage")
    @NotNull({"page","limit"})
    public ReturnData<PageData<OrderPaymentCost>> getOrderPaymentPage(OrderPaymentCost query,Integer page, Integer limit) {
        PageData<OrderPaymentCost> orderPaymentPage = orderPaymentCostService.getOrderPaymentPage(query,getLoginUser(),page,limit);
        return jsonView(SUCCESS_CODE,"获取预支付单成功",orderPaymentPage);
    }

    @PostMapping(value = "getOrderSettlementPage")
    @NotNull({"page","limit"})
    public ReturnData<PageData<OrderSettlementCost>> getOrderSettlementPage(OrderSettlementCost query, Integer page, Integer limit) {
        PageData<OrderSettlementCost> orderPaymentPage = orderSettlementCostService.getOrderSettlementPage(query,getLoginUser(),page,limit);
        return jsonView(SUCCESS_CODE,"获取预结算单成功",orderPaymentPage);
    }

    @PostMapping(value = "getOrderRefundPage")
    @NotNull({"page","limit"})
    public ReturnData<PageData<OrderRefundCost>> getOrderRefundPage(OrderRefundCost query, Integer page, Integer limit) {
        PageData<OrderRefundCost> orderRefundPage = orderRefundCostService.getOrderRefundPage(query,getLoginUser(),page,limit);
        return jsonView(SUCCESS_CODE,"获取预退款单成功",orderRefundPage);
    }

    @PostMapping(value = "getServicePaymentPage")
    @NotNull({"page","limit"})
    public ReturnData<PageData<ServicePaymentCost>> getServicePaymentPage(ServicePaymentCost query, Integer page, Integer limit) {
        PageData<ServicePaymentCost> orderRefundPage = servicePaymentCostService.getServicePaymentCostPage(query,getLoginUser(),page,limit);
        return jsonView(SUCCESS_CODE,"获取服务费用单成功",orderRefundPage);
    }

    //--------------------------------------------------------------------------------------------------------------------------------------------

    @PostMapping(value = "getOrderPaymentById")
    @NotNull({"paymentOrderId"})
    public ReturnData<OrderPaymentCost> getOrderPaymentById(String paymentOrderId) {
        OrderPaymentCost paymentById = orderPaymentCostService.getOrderPaymentById(paymentOrderId);
        return jsonView(SUCCESS_CODE,"获取预支付单成功",paymentById);
    }

    @PostMapping(value = "getOrderPaymentListByIds")
    @NotNull({"paymentOrderIds"})
    public ReturnData<List<OrderPaymentCost>> getOrderPaymentListByIds(String[] paymentOrderIds) {
        List<String> orderPaymentIds = ArrayUtil.initList(paymentOrderIds);
        List<OrderPaymentCost> list = orderPaymentCostService.getOrderPaymentListByIds(orderPaymentIds);
        return jsonView(SUCCESS_CODE,"获取预支付单成功",list);
    }

    @PostMapping(value = "getOrderSettlementListByIds")
    @NotNull({"settlementOrderIds"})
    public ReturnData<List<OrderSettlementCost>> getOrderSettlementListByIds(String[] settlementOrderIds) {
        List<String> orderSettlementIds = ArrayUtil.initList(settlementOrderIds);
        List<OrderSettlementCost> list = orderSettlementCostService.getOrderSettlementListByIds(orderSettlementIds);
        return jsonView(SUCCESS_CODE,"获取预结算单成功",list);
    }

    @PostMapping(value = "getOrderSettlementListByTransOrderIds")
    @NotNull({"transOrderIds"})
    public ReturnData<List<OrderSettlementCost>> getOrderSettlementListByTransOrderIds(String[] transOrderIds) {
        List<String> transOrderIdList = ArrayUtil.initList(transOrderIds);
        List<OrderSettlementCost> list = orderSettlementCostService.getOrderSettlementListByTransOrderIds(transOrderIdList);
        return jsonView(SUCCESS_CODE,"获取预结算单成功",list);
    }

    @PostMapping(value = "getOrderRefundListByIds")
    @NotNull({"refundOrderIds"})
    public ReturnData<List<OrderRefundCost>> getOrderRefundListByIds(String[] refundOrderIds) {
        List<String> orderRefundCostIds = ArrayUtil.initList(refundOrderIds);
        List<OrderRefundCost> list = orderRefundCostService.getOrderRefundListByIds(orderRefundCostIds);
        return jsonView(SUCCESS_CODE,"获取预退款单成功",list);
    }
    @PostMapping(value = "getOrderRefundById")
    @NotNull({"refundOrderId"})
    public ReturnData<OrderRefundCost> getOrderRefundById(String refundOrderId) {
        OrderRefundCost orderRefundCost = orderRefundCostService.getOrderRefundById(refundOrderId);
        return jsonView(SUCCESS_CODE,"获取预退款单成功",orderRefundCost);
    }

    @PostMapping(value = "getServicePaymentListByIds")
    @NotNull({"paymentOrderIds"})
    public ReturnData<List<ServicePaymentCost>> getServicePaymentListByIds(String[] servicePaymentIds) {
        List<String> servicePaymentIdList = ArrayUtil.initList(servicePaymentIds);
        List<ServicePaymentCost> list = servicePaymentCostService.getServicePaymentListByIds(servicePaymentIdList);
        return jsonView(SUCCESS_CODE,"获取预退款单成功",list);
    }


    //---------------------------------------------------------------------------------------------------------------------------------------------

    @PostMapping(value = "getOrderPaymentByGoodsOrderId")
    @NotNull({"goodsOrderId"})
    public ReturnData<OrderPaymentCost> getOrderPaymentByGoodsOrderId(String goodsOrderId) {
        OrderPaymentCost orderPaymentCost = orderPaymentCostService.getOrderPaymentByGoodsOrderId(goodsOrderId);
        return jsonView(SUCCESS_CODE,"获取预支付单成功",orderPaymentCost);
    }

    @PostMapping(value = "getOrderPaymentByRobOrderId")
    @NotNull({"robOrderId"})
    public ReturnData<OrderPaymentCost> getOrderPaymentByRobOrderId(String robOrderId) {
        OrderPaymentCost orderPaymentCost = orderPaymentCostService.getOrderPaymentByRobOrderId(robOrderId);
        return jsonView(SUCCESS_CODE,"获取预支付单成功",orderPaymentCost);
    }

    @PostMapping(value = "getOrderPaymentByTransitOrderId")
    @NotNull({"transitOrderId", "paySite"})
    public ReturnData<OrderPaymentCost> getOrderPaymentByTransitOrderId(String transitOrderId, OrderPaymentCost.PaySite paySite) {
        OrderPaymentCost orderPaymentCost = orderPaymentCostService.getOrderPaymentByTransitOrderId(transitOrderId,paySite);
        return jsonView(SUCCESS_CODE,"获取预支付单成功",orderPaymentCost);
    }

    //--------------------------------------------------------------------------------------------------------------------------------------------
    /**
     * 生成技术服务费
     * */
//    @PostMapping(value = "generateServicePayment")
//    @NotNull({"tradeRecordId"})
//    public ReturnData<OrderPaymentCost> generateServicePayment(String tradeRecordId) {
//        List<TradeRecord> tradeRecords = ParamUtil.isSuccess(capitalRemote.getTradeRecordListByIds(tradeRecordId));
//        TradeRecord tradeRecord = ArrayUtil.first(tradeRecords);
//        servicePaymentCostService.generateServicePayment(ServicePaymentCost.ServicePlatType.whPlat,tradeRecord);
//        servicePaymentCostService.generateServicePayment(ServicePaymentCost.ServicePlatType.fhPlat,tradeRecord);
//        return jsonView(SUCCESS_CODE,"生成服务费用单成功");
//    }

    //---------------------------------------------------------------------------------------------------------------------------------------------

    /**
     * 修改费用
     * */
    @PostMapping(value = "updateOrderRefund")
    @NotNull({"refundInsuranceCost", "refundAdditionalCost","refundTransportCost","refundTaxCost","refundShipperFeeCost"})
    public ReturnData<OrderRefundCost> updateOrderRefund(OrderRefundCost formRefund) {
         orderRefundCostService.updateOrderRefund(getLoginUser(),formRefund);
        return jsonView(SUCCESS_CODE,"操作成功");
    }

    /**
     * 锁定/解锁
     * */
    @PostMapping(value = "lockOrderRefund")
    @NotNull({"paymentOrderId", "locking"})
    @SafeVerify
    public ReturnData<Object> lockOrderRefund(@Explain("退款单ID") String paymentOrderId, @Explain("是否锁定") Boolean locking) {
        orderRefundCostService.lockOrderRefund( getLoginUser(),  paymentOrderId, locking);
        return jsonView(true, locking ? "锁定成功" : "解锁成功");
    }

    /**
     * 确认审核
     * */
    @PostMapping(value = "sureOrderRefund")
    @NotNull({"refundInsuranceCost", "refundAdditionalCost","refundTransportCost","refundTaxCost","refundShipperFeeCost"})
    public ReturnData<Object> sureOrderRefund(OrderRefundCost formRefund) {
        orderRefundCostService.sureOrderRefund( getLoginUser(), formRefund);
        return jsonView(SUCCESS_CODE,"操作成功");
    }



}
